package cn.imvc.app.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.imvc.app.domains.GoodsQueryBean;
import cn.imvc.app.entities.Goods;
import cn.imvc.app.entities.GoodsSku;
import cn.imvc.app.models.GoodsModel;
import cn.imvc.app.models.GoodsSkuModel;
import cn.imvc.app.models.IndexModel;

@Controller
@RequestMapping("/goods")
public class GoodsControllerImpl  implements GoodsController {
	@Autowired
	private GoodsModel goodsModel;
	@Autowired
	private GoodsSkuModel goodsSkuModel;
	@Autowired
	private IndexModel indexModel;

	@RequestMapping("/listByClass/{goodsClassId}/{page}/{sort}.html")
	public String listByClass(@PathVariable Integer goodsClassId, @PathVariable Integer page, @PathVariable String sort, Model model) {
		//Page<Object[]> pageResult = goodsModel.findPageResult(goodsClassId, page, sort);
		Page<Goods> pageResult = goodsModel.findByGoodsClassId(goodsClassId, page, sort);
		model.addAttribute("pageResult", pageResult);
		return "goods/list_by_class";
	}

	@RequestMapping("/listByClass/{goodsClassId}/{page}.html")
	public String listByClass(@PathVariable Integer goodsClassId, @PathVariable Integer page, Model model) {
		return listByClass(goodsClassId, page, null, model);
	}

	@RequestMapping("/listByClass/{goodsClassId}.html")
	public String listByClass(@PathVariable Integer goodsClassId, Model model) {
		return listByClass(goodsClassId, null, null, model);
	}

	@RequestMapping("/listByClass/{goodsClassId}/0/{sort}.html")
	public String listByClass(@PathVariable Integer goodsClassId, @PathVariable String sort, Model model) {
		return listByClass(goodsClassId, null, sort, model);
	}

	@RequestMapping("/{goodsId}.html")
	public String showDetail(@PathVariable Integer goodsId, Model model) {
		List<GoodsSku> goodsSkus = goodsSkuModel.findByGoodsId(goodsId);
		Integer skuId = null;
		Map<String, Object> result = null;
		if (goodsSkus != null && goodsSkus.size() > 0) {
			skuId = goodsSkus.get(0).getId();
			//手机端
			int platform =1;
			result = goodsSkuModel.showDetail(skuId,platform);
			model.addAllAttributes(result);
		}
		if(result==null||result.size()==0){
			return "redirect:/index.html";
		}
		return "goods/detail";
	}

	@RequestMapping("/findPageResult.json")
	public @ResponseBody Page<Object[]> findPageResult(Integer goodsClassId, Integer page, String sort) {
		return goodsModel.findPageResult(goodsClassId, page, sort);
	}
	@RequestMapping("/findPageResult1.html")
	public String findPageResult1(Integer goodsClassId, Integer page, String sort, Model model) {
		//Page<Object[]> pageResult = goodsModel.findPageResult(goodsClassId, page, sort);
		//model.addAttribute("pageResult", pageResult);
		Page<Goods> pageResult = goodsModel.findByGoodsClassId(goodsClassId, page, sort);
		model.addAttribute("pageResult", pageResult);
		return "goods/list_by_class";
	}
	@RequestMapping("/findPageResult.html")
	public String findPageResult(GoodsQueryBean goodsQueryBean, Model model) {
		Page<Goods> pageResult = goodsModel.cloudSearchGoods(goodsQueryBean);
		model.addAttribute("pageResult", pageResult);
		model.addAttribute("goodsQueryBean", goodsQueryBean);
		Integer showType =106;
		byte platform = 1;
		Map<String,Object> paramMap = new HashMap<>();
		paramMap.put("showType", showType);
		paramMap.put("platform", platform);
		Map<String,Object> headerInfos = indexModel.getHeaderInfos(paramMap);
		model.addAllAttributes(headerInfos);
		return "goods/list_by_class";
	}
	//-----------pc--------------
	@RequestMapping("/list.htm")
	public String pcList(GoodsQueryBean goodsQueryBean, Model model) {
		Map<String,Object> searchFilters = goodsModel.getSearchFilters(goodsQueryBean.getGoodsClassId());
		GoodsQueryBean showGoodsQueryBean = goodsModel.getShowParams(goodsQueryBean);
		model.addAllAttributes(searchFilters);
		showGoodsQueryBean.setGoodsClassName((String)searchFilters.get("goodsClassName"));
		model.addAttribute("goodsQueryBean", showGoodsQueryBean);
		return "goods/pclist";
	}
	@RequestMapping("/findPageResult.htm")
	public String pcFindPageResult(GoodsQueryBean goodsQueryBean, Model model) {
		Integer page = goodsQueryBean.getPage();
		if(page!=null&&page>0){
			goodsQueryBean.setPage(page-1);
		}
		//Page<Object[]> pageResult = goodsModel.findPageResult(goodsQueryBean);
		Page<Goods> pageResult = goodsModel.cloudSearchGoods(goodsQueryBean);
		model.addAttribute("pageResult", pageResult);
		return "goods/pclist_content";
	}
	@RequestMapping("/{goodsId}.htm")
	public String pcShowDetail(@PathVariable Integer goodsId, Model model) {
		List<GoodsSku> goodsSkus = goodsSkuModel.findByGoodsId(goodsId);
		Integer skuId = null;
		Map<String, Object> result = null;
		if (goodsSkus != null && goodsSkus.size() > 0) {
			skuId = goodsSkus.get(0).getId();
			//pc端
			int platform =2;
			result = goodsSkuModel.showDetail(skuId,platform);
			model.addAllAttributes(result);
		}
		if(result==null||result.size()==0){
			return "redirect:/index.htm";
		}
		return "goods/pcdetail";
	}
}
